/**
 * AlumniAdminController
 * 
 * Use of Class.js
 * 
 * @author david.liu
 */
var DataTable = Class.extend({
	columnDefs : [],
	data : [],
	sortField : undefined,
	reverse : false,
	limit : 6,
	startNumber : 0,

	/**
	 * Initialize BreadcrumbService
	 */
	init : function(columnDefs, data) {
		this.columnDefs = columnDefs || [];
		this.data = data || []
	},
	
	sort : function(fieldName, sortable) {
		var sb = sortable || false;
		if (!sb) {
			return '';
		}

//		if (this.sortField == fieldName) {
			this.sortField = fieldName;
			this.reverse = !this.reverse;
//		} else {
//			this.sortField = fieldName;
//			this.reverse = false;
//		}
	},
	
	sortingClass : function(fieldName, sortable) {
		var sb = sortable || false;
		if (!sb) {
			return '';
		}

		if (this.sortField == undefined
				|| this.sortField != fieldName) {
			return 'sorting';
		} else if (this.reverse) {
			return 'sorting_desc';
		} else {
			return 'sorting_asc';
		}
	},
	
	
	endNumber : function() {
		if(this.startNumber + this.limit < this.data.length) {
			return this.startNumber + this.limit;
		} else {
			return this.data.length;
		}
		
	},
	
	currentData : function() {
		return this.data.slice(this.startNumber, this.endNumber());
	},
	
	selectPrevious : function() {
		if(this.startNumber - this.limit > 0) {
			this.startNumber =  this.startNumber - this.limit;
		} else {
			this.startNumber = 0;
		}
	},
	
	selectNext : function() {
		if(this.startNumber + this.limit < this.data.length) {
			this.startNumber =  this.startNumber + this.limit;
		} else {
			this.startNumber = 0;
		}
	},
	
	noPrevious : function() {
		return this.startNumber - this.limit < 0;
	},
	
	noNext : function() {
		return this.startNumber + this.limit >= this.data.length;
	}
	
});

var AlumniListController = BaseController.extend({

	_alimniModel : null,
	_dataTable : null,

	/**
	 * 初始化应用控制器
	 * 
	 * @param $scope,
	 *            当前控制器的scope
	 * 
	 */
	init : function($scope, alumniModel) {
		this._alimniModel = alumniModel;
		this._super($scope)
		
	},

	/**
	 * @Override
	 */
	defineScope : function() {
		this.$scope.testText = 'helllo';

		this.$scope.alumnuiTable = new DataTable([ {
			field : 'id',
			displayName : 'ID',
			sortable : true,
			show : true
		}, {
			field : 'name',
			displayName : '姓名',
			sortable : true,
			show : true
		}, {
			field : 'college',
			displayName : '学院',
			sortable : false,
			show : false
		} ]);

		this._alimniModel.get(function(data, status, headers, config) {
			this.$scope.alumnuiTable.data = data;
		}.bind(this));

		this.$scope.deleteAlumnus = this.deleteAlumnus.bind(this);

		this._handleErrorMessage();
	},

	deleteAlumnus : function(alumnus) {
		this._alimniModel.remove(100, function() {
		}, this._handleErrorCallbackWithMessage.bind(this));
	}

})

AlumniListController.$inject = [ '$scope', 'AlumniModel' ];